Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't run cleanups twice in "if true" blocks #10735

Merged
merged 1 commit into from
Nov 30, 2013

Conversation

alexcrichton
Copy link
Member

Turns out with_scope already translates destructors, so by manually
translating destructors we end up running them all twice (bad).

Closes #10734

Turns out `with_scope` already translates destructors, so by manually
translating destructors we end up running them all twice (bad).

Closes rust-lang#10734

pub fn main() {
if true {
let _a = ~3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems not quite ideal to rely on throwing a pointer being freed was not allocated malloc error to indicate the bug exists. I would suggest something like the following:

#[unsafe_no_drop_flag]
struct Foo {
    dropped: bool
}

impl Drop for Foo {
    fn drop(&mut self) {
        assert!(!self.dropped);
        self.dropped = true;
    }
}

pub fn main() {
    if true {
        let _a = Foo{ dropped: false };
    }

    if false {
        fail!();
    } else {
        let _a = Foo{ dropped: false };
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After chatting with @cmr, it might actually be nice to assert that it's dropping at all, just to kill two birds with one stone (i.e. define this test as checking that it drops the correct number of times, rather than checking that it hasn't dropped too many times).

static mut drop_count: uint = 0;

#[unsafe_no_drop_flag]
struct Foo {
    dropped: bool
}

impl Drop for Foo {
    fn drop(&mut self) {
        assert!(!self.dropped);
        self.dropped = true;
        unsafe { drop_count += 1; }
    }
}

pub fn main() {
    if true {
        let _a = Foo{ dropped: false };
    }
    unsafe { assert!(drop_count == 1); }

    if false {
        fail!();
    } else {
        let _a = Foo{ dropped: false };
    }
    unsafe { assert!(drop_count == 2); }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, come to think about it, this is not quite right as it's making a (probably bad) assumption about where the drops occur. It should probably be something like this instead:

pub fn main() {
    {
        if true {
            let _a = Foo{ dropped: false };
        }

        if false {
            fail!();
        } else {
            let _a = Foo{ dropped: false };
        }
    }
    unsafe { assert!(drop_count == 2); }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I'm having a conversation with myself.

After thinking some more, the previous version (with the two asserts) isn't so bad, because that's also checking to make sure that if true { expr } behaves like { expr } rather than behaving like expr, which seems to me to be the correct interpretation. FWIW, that version (again, the one with the two asserts) passes with this patch applied.

bors added a commit that referenced this pull request Nov 30, 2013
Turns out `with_scope` already translates destructors, so by manually
translating destructors we end up running them all twice (bad).

Closes #10734
@bors bors closed this Nov 30, 2013
@bors bors merged commit 7bb166e into rust-lang:master Nov 30, 2013
@alexcrichton alexcrichton deleted the issue-10734 branch December 1, 2013 20:45
flip1995 pushed a commit to flip1995/rust that referenced this pull request May 20, 2023
Bump README copyright

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

if true { ... } runs destructors twice
4 participants